package top.zhaoyuliu.logbook.server.connect.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.tio.core.ChannelContext;

import cn.hutool.json.JSONUtil;
import lombok.extern.slf4j.Slf4j;
import top.zhaoyuliu.logbook.client.connect.packet.PacketType;
import top.zhaoyuliu.logbook.server.connect.builder.ElectionBuilder;
import top.zhaoyuliu.logbook.server.connect.client.ClientStart;

/**
 * Copyright © 2021 Arain. All rights reserved.
 * 
 * import org.springframework.stereotype.Service; *
 * 
 * @Title: HeartBeatService.java
 * @Prject: logbook-server
 * @Package: top.zhaoyuliu.logbook.server.connect.service
 * @Description: <功能详细描述>
 * @author: Arain
 * @date: 2021年1月25日 下午5:11:32
 * @version: V1.0
 */
@Service
@Slf4j
public class HeartBeatService implements DataService {

	@Autowired
	private ClientStart clientStart;

	@Override
	public Byte getType() {
		return PacketType.HEART_BEAT;
	}

	@Override
	public void handler(String data, ChannelContext channelContext) {
		ElectionBuilder builder = JSONUtil.toBean(data, ElectionBuilder.class);
		String addr = builder.getAddr();
		int taskNum = builder.getTaskNum();
		log.info("{} -> 节点心跳  ,任务数 -> {}", addr, taskNum);
		clientStart.getNodeTask().put(addr, taskNum);
	}
}
